package com.zcsy.manage.web.mall;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.manage.model.mall.AdCategory;
import com.zcsy.manage.model.system.Dict;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.service.mall.IAdCategoryService;
import com.zcsy.manage.service.system.IDictService;
import com.zcsy.manage.web.common.LogUtils;

/**
 * 
 * @Title: AdCategoryController.java
 * @Package： com.zcsy.manage.web.mall
 * @Description: 广告位类型管理控制类
 * @author lilan 1063972142@qq.com
 * @Copyright: 2015 武汉恒数利通信息技术有限公司 All rights reserved.
 * @date 2016年4月20日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
@RequestMapping(value = "/adCategory")
public class AdCategoryController {

	@Autowired
	private IAdCategoryService adCategoryService;

	@Autowired
	private IDictService dictService;

	/**
	 * 
	 * @Description 返回广告位类型查询列表<br>
	 * 
	 * @param model
	 * @param pNo
	 *            当前页面
	 * @param name
	 *            广告位类型
	 * @param req
	 *            用户请求
	 * @return
	 * @author lilan 1063972142@qq.com
	 * @date 2016年4月20日
	 * @see
	 */
	@RequestMapping(value = "adCategoryList", method = RequestMethod.GET)
	public String adCategoryList(Model model, Integer pNo, Integer pSize,
			String name, HttpServletRequest req) {
		if (pNo == null) {
			pNo = Constants.PAGE_NO;
		}

		Integer pageSize;
		if (pSize == null) {
			pageSize = Constants.PAGE_SIZE;
		} else {
			switch (pSize) {
			case 0:
				pageSize = Constants.PAGE_SIZE;
				break;
			case 1:
				pageSize = Constants.PAGE_SIZE_TWENTY;
				break;
			case 2:
				pageSize = Constants.PAGE_SIZE_FIFTY;
				break;
			default:
				pageSize = Constants.PAGE_SIZE;
				break;
			}
		}

		// 此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
		PageHelper.startPage(pNo, pageSize);

		AdCategory adCategory = new AdCategory();
		if (StringUtils.isNotEmpty(name)) {
			adCategory.setName(name);
		}
		List<AdCategory> adCategories = adCategoryService
				.selectByKeyword(adCategory);
		PageInfo<AdCategory> info = new PageInfo<AdCategory>(adCategories);

		model.addAttribute("adCategory", info);
		model.addAttribute("currentPage", pNo);
		model.addAttribute("pSize", pSize);
		model.addAttribute("name", name);

		List<Dict> pageSizes = dictService.getDictFromCache("pageSize");
		model.addAttribute("pageSizes", pageSizes);

		return "mall/adcategory/adCategoryList";
	}

	/**
	 * 
	 * @Description 返回新增或编辑页面<br>
	 * 
	 * @param model
	 * @param id
	 *            主键id
	 * @return
	 * @author lilan 1063972142@qq.com
	 * @date 2016年4月20日
	 * @see
	 */
	@RequestMapping(value = "editAdCategory", method = RequestMethod.GET)
	public String selectById(Model model, String id) {
		try {
			if (String.valueOf(Constants.STATE_DISABLED).equals(id)) {
				model.addAttribute("type", Constants.SAVE_TYPE);
			} else {
				AdCategory adCategory = adCategoryService
						.selectByPrimaryKey(id);
				if (adCategory != null) {
					model.addAttribute("type", Constants.EDIT_TYPE);
					model.addAttribute("adCategory", adCategory);
				}
			}
		} catch (Exception e) {
		}

		return "mall/adcategory/adCategoryEdit";
	}

	/**
	 * @Description 增加或编辑广告位类型
	 * @param id
	 *            主键id
	 * @param name
	 *            广告位类型
	 * @param width
	 *            图片最佳宽度
	 * @param height
	 *            图片最佳高度
	 * @param memo
	 *            备注
	 * @param descrip
	 *            描述
	 * @param req
	 *            用户请求
	 * @return
	 * @author lilan 1063972142@qq.com
	 * @date 2016年4月20日
	 * @see
	 */
	@RequestMapping(value = "saveorUpdateAdCategory", method = RequestMethod.POST)
	@ResponseBody
	public RequestJson saveOrUpdateAdCategory(Model model, String id,
			String name, String value, Integer width, Integer height,
			String memo, String descrip, String img, HttpServletRequest req) {
		RequestJson result = new RequestJson();
		User user = (User) req.getSession().getAttribute(
				RequestConstants.USER_SESSION_ID);
		AdCategory adCategory = new AdCategory();

		adCategory.setName(name);
		adCategory.setValue(value);
		adCategory.setImgWidth(width);
		adCategory.setImgHeight(height);
		adCategory.setMemo(memo);
		adCategory.setDescrip(descrip);
		adCategory.setDefaultImg(img);

		try {
			if (StringUtils.isNotEmpty(id)) {
				adCategory.setId(id);
				adCategoryService.update(adCategory);
				LogUtils.insertLog("用户编辑广告位分类信息", user.getId(), req);
			} else {
				adCategory.setId(UniqueNumberUtils.get18UniqueNumber());
				adCategoryService.insert(adCategory);
				LogUtils.insertLog("用户新增广告位分类信息", user.getId(), req);
			}
			result.setSuccess(true);
		} catch (Exception e) {
			result.setMsg("操作失败！");
		}

		return result;
	}

	/**
	 * @Description 根据主键id删除多条广告位类型记录<br>
	 * @param ids
	 *            广告位类型对象id数组
	 * @param req
	 *            用户请求
	 * @author lilan 1063972142@qq.com
	 * @date 2016年4月20日
	 * @see
	 */
	@RequestMapping(value = "deleteByPrimaryKey", method = RequestMethod.POST)
	@ResponseBody
	public RequestJson deleteByPrimaryKey(String ids, HttpServletRequest req) {
		RequestJson result = new RequestJson();
		User user = (User) req.getSession().getAttribute(
				RequestConstants.USER_SESSION_ID);
		try {
			String keys[] = ids.split("\\,");
			for (int i = 0; i < keys.length; i++) {
				adCategoryService.deleteByPrimaryKey(keys[i]);
			}
			LogUtils.insertLog("用户删除广告位分类信息", user.getId(), req);
			result.setSuccess(true);
		} catch (NumberFormatException e) {
			result.setSuccess(false);
			result.setMsg("操作失败");
		}

		return result;
	}

	/**
	 * @Description 根据主键id查询广告分类
	 * @param adCategoryid 广告分类主键id
	 * @return 对应广告分类对象
	 * @author liuying 1075817474@qq.com
	 * @date 2016年9月13日
	 * @see 
	 */
	@RequestMapping(value = "selectAdCategoryByPrimaryKey", method = RequestMethod.GET)
	@ResponseBody
    public RequestJson getGoodsInfoByShopId(String adCategoryid){
    	RequestJson result = new RequestJson();
    	try {
    		AdCategory adCategory = adCategoryService.selectByPrimaryKey(adCategoryid);
    		result.setObj(adCategory);
    	} catch (Exception e) {
    		result.setMsg("操作异常");
    		result.setSuccess(false);
    	}
    	return result;
    }
}
